home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 November / macformat-030.iso / Shareware City / Developers / PICTButton CDEF 1.1 / PICTButton.c < prev   
Encoding:
C/C++ Source or Header  |  1995-08-04  |  5.3 KB  |  242 lines  |  [TEXT/MMCC]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     PICTButton CDEF
  4.     version 1.1
  5.     4 August 1995
  6.     
  7.     Written by: Paul Celestin
  8.     
  9.     This CDEF displays a picture whose resource ID is derived from
  10.     the value field of the CNTL. The min, max, and refcon fields are
  11.     not used and should therefore not be set.
  12.  
  13.     950804 - 1.1.0  finally converted this thing over to CodeWarrior!
  14.     
  15.     940703 - 1.0.7    now works over multiple monitors of different bit
  16.                     depths
  17.  
  18.     940319 - 1.0.6    removed unnecessary erasing code
  19.     
  20.     931012 - 1.0.5    really fixed problem with black and white PICTs
  21.                     showing up properly
  22.     
  23.     931012 - 1.0.4    changed color of text in variation 2 to blue
  24.     
  25.     930922 - 1.0.3    fixed a problem with black and white PICTs showing
  26.                     up, or so I thought
  27.     
  28.     930915 - 1.0.2    fixed the new variations
  29.     
  30.     930829 - 1.0.1     added variations 1 and 2
  31.     
  32.     930822 - 1.0.0    initial release
  33.  
  34. ---------------------------------------------------------------------- */
  35.  
  36. # include <Controls.h>
  37. # include <Fonts.h>
  38. # include <OSUtils.h>
  39. # include <QuickDraw.h>
  40. # include <Resources.h>
  41. # include <ToolUtils.h>
  42. # include <Traps.h>
  43.  
  44.  
  45. // defines
  46.  
  47. # define DEFAULTPICT    0
  48. # define INVERTEDPICT    1
  49. # define BW_OFFSET        2
  50.  
  51.  
  52. // prototypes
  53.  
  54. pascal long main(short variation, ControlHandle theControl, short message, long param);
  55. int hasColor(Rect);
  56. void drawIt(ControlHandle control,short variation);
  57. long testIt(ControlHandle control, Point myPoint);
  58.  
  59.  
  60. // here's the main stuff
  61.  
  62. pascal long main(short variation, ControlHandle theControl, short message, long param)
  63. {
  64.     long    returnValue = 0L;
  65.     char    state = HGetState((Handle)theControl);
  66.  
  67.     switch(message)
  68.     {
  69.         case drawCntl:
  70.             drawIt(theControl,variation);
  71.         case testCntl:
  72.             returnValue = testIt(theControl, *(Point *) ¶m);
  73.         case calcCRgns:
  74.             break;
  75.           case initCntl:
  76.               break;
  77.         case dispCntl:
  78.             break;
  79.         case posCntl:
  80.             break;
  81.         case thumbCntl:
  82.             break;
  83.         case dragCntl:
  84.             break;
  85.         case autoTrack:
  86.             break;
  87.         case calcCntlRgn:
  88.             break;
  89.         case calcThumbRgn:
  90.             break;
  91.         default:
  92.             break;
  93.     }
  94.  
  95.     HSetState((Handle)theControl,state);
  96.  
  97.     return(returnValue);                /* tell them what happened */
  98. }
  99.  
  100.  
  101. /* ----------------------------------------------------------------------
  102. hasColor
  103. ---------------------------------------------------------------------- */
  104. int hasColor(Rect r)
  105.  
  106. {
  107.     SysEnvRec        myComputer;
  108.     GDHandle        curDev;
  109.     PixMapHandle    myPixMap;
  110.     
  111.     SysEnvirons(2,&myComputer);
  112.     if (myComputer.hasColorQD)
  113.     {
  114.         LocalToGlobal((Point*) &r);
  115.         LocalToGlobal(1 + (Point*) &r);
  116.         curDev = GetMaxDevice(&r);
  117.         myPixMap = (**curDev).gdPMap;
  118.         if ((**myPixMap).pixelSize > 1)
  119.             return(1);
  120.         else
  121.             return(0);
  122.     }
  123.     return(0);
  124. }
  125.  
  126.  
  127. /* ----------------------------------------------------------------------
  128. drawIt - here is where we actually draw the control
  129. ---------------------------------------------------------------------- */
  130. void drawIt(ControlHandle control,short variation)
  131.  
  132. {
  133.     Rect                 myRect;
  134.     short                 myPictID;
  135.     GrafPtr                myPort;
  136.     PicHandle            myPicture;
  137.     PenState            oldPenState;
  138.     Str255                myTitle;
  139.     int                    savedFont,
  140.                         savedSize,
  141.                         savedMode;
  142.     Pattern                myGray;
  143.  
  144.     GetPort(&myPort);                        /* save off the current port */
  145.  
  146.     if (!(*control)->contrlVis)                /* if not visible, do nothing */
  147.         return;
  148.  
  149.     myPictID = GetCtlValue(control);        /* base ID is stored in the value field */
  150.         
  151.     myRect = (*control)->contrlRect;
  152.     
  153.     if (!hasColor(myRect))                    /* use black and white PICTs */
  154.         myPictID = myPictID + BW_OFFSET;
  155.     
  156.     if ((*control)->contrlHilite == inButton)
  157.         myPictID = myPictID + INVERTEDPICT;    /* invert while tracking */
  158.     
  159.     myPicture = (PicHandle)GetResource('PICT', myPictID);
  160.     
  161.     if ( myPicture == 0L )                    /* could not find the PICT */
  162.         return;
  163.  
  164.     LoadResource((Handle)myPicture);
  165.     HNoPurge((Handle)myPicture);
  166.  
  167.     DrawPicture(myPicture, &myRect);        /* draw the picture */
  168.     
  169.     switch (variation)
  170.     {
  171.     case 1:                                    /* display title of control in geneva 9 */
  172.         {
  173.             savedFont = myPort->txFont;        /* save off current values */
  174.             savedSize = myPort->txSize;
  175.             savedMode = myPort->txMode;
  176.             
  177.             ForeColor(blueColor);
  178.             TextFont(geneva);
  179.             TextSize(9);
  180.             TextMode(srcOr);
  181.             
  182.             BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);
  183.             
  184.             MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2
  185.                 ,myRect.bottom - 6);
  186.             DrawString(myTitle);
  187.             
  188.             TextFont(savedFont);            /* restore saved values */
  189.             TextSize(savedSize);
  190.             TextMode(savedMode);
  191.             ForeColor(blackColor);
  192.             
  193.             break;
  194.         }
  195.     case 2:                                    /* simple 2-pixel wide black border */
  196.         {
  197.             PenSize(2,2);
  198.             InsetRect(&myRect,-3,-3);
  199.             FrameRect(&myRect);
  200.             InsetRect(&myRect,3,3);
  201.             PenSize(1,1);
  202.             break;
  203.         }
  204.     }
  205.  
  206.     if ((*control)->contrlHilite == 255)    /* gray out the picture */
  207.     {
  208.         GetPenState(&oldPenState);
  209.         PenNormal();
  210.         GetIndPattern(&myGray,0,4);
  211.         PenPat(&myGray);
  212.         PenMode(patBic);
  213.         PaintRect(&myRect);
  214.         SetPenState(&oldPenState);
  215.     }
  216.     
  217.     HPurge((Handle)myPicture);
  218.     
  219.     return;                                    /* we are done drawing */
  220. }
  221.  
  222.  
  223. /* ----------------------------------------------------------------------
  224. testIt - test for mouse hits within control
  225. ---------------------------------------------------------------------- */
  226. long testIt(ControlHandle control, Point myPoint)
  227.  
  228. {
  229.     Rect myRect;
  230.  
  231.     myRect = (*control)->contrlRect;
  232.     
  233.     if    (
  234.         ((*control)->contrlVis != 0) &&
  235.         ((*control)->contrlHilite != 255) &&
  236.         (PtInRect(myPoint,&myRect))
  237.         )
  238.         return(inButton);
  239.     else
  240.         return(0);
  241. }
  242.